package facilius.controller.action;

import facilius.model.ServiceLocator;
import facilius.model.pojo.Aula;
import facilius.model.pojo.Material;
import java.io.File;
import org.mentawai.core.BaseAction;

/**
 *
 * @author Graciano
 */
public class RemoveFileAction extends BaseAction{

    @Override
    public String execute() throws Exception {
        String consequence = ERROR;
        try {
            Long id = input.getLong("id");
            Material material = ServiceLocator.getMaterialService().readById(id);
            Long aulaId = material.getAula().getId();
            Aula aula = ServiceLocator.getAulaService().readById(aulaId);
            String path = org.mentawai.core.ApplicationManager.getRealPath() + "\\WEB-INF\\files\\" + aula.getId() + "\\" + material.getNome();

            File file = new File(path);
            if (file.exists()){
                file.delete();
            }
            ServiceLocator.getMaterialService().delete(material.getId());
            consequence =  SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return consequence;
    }



}
